﻿
using JinfuyuanProject.Entity;
using JinfuyuanProject.Repository.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace JinfuyuanProject.Repository
{
    public class ExpenseRecordRepository:IExpenseRecordRepository
    {
        private readonly JinfuyuanDbContext _db;

        public ExpenseRecordRepository(JinfuyuanDbContext db)
        {
            _db=db;
        }
        public bool Delete(ExpenseRecord expenseRecord)
        {
            try
            {
                _db.Entry(expenseRecord).State = System.Data.Entity.EntityState.Deleted;
                return _db.SaveChanges() > 0;
            }
            catch
            {
                return false;
            }
        }

        public IQueryable<ExpenseRecord> GetList()
        {
            try
            {
                return _db.ExpenseRecord;
            }
            catch
            {
                return null;
            }
        }

        public ExpenseRecord GetModel(int Id)
        {
            try
            {
                ExpenseRecord expenseRecord = _db.ExpenseRecord.Find(Id);
                return expenseRecord;
            }
            catch
            {
                return null;
            }
        }

        public int Insert(ExpenseRecord expenseRecord)
        {
            try
            {
                _db.Entry(expenseRecord).State = System.Data.Entity.EntityState.Added;
                _db.SaveChanges();
                return expenseRecord.Id;
            }
            catch (Exception ex)
            {
                return 0;
            }
        }

        public bool Update(ExpenseRecord expenseRecord)
        {
            try
            {
                _db.Entry(expenseRecord).State = System.Data.Entity.EntityState.Modified;
                return _db.SaveChanges() > 0;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
    }
}
